import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import '@vant/touch-emulator'
import vueFilter from './untils/filter'
import axios from 'axios'
import qs from 'qs'
import { Lazyload, Toast } from 'vant'
for (const key in vueFilter) {
  Vue.filter(key, vueFilter[key])
}

Vue.use(Lazyload, {
  preLoad: 1,
  error: './dist/404.png',
  loading: './dist/loading.gif',
  attempt: 1,
  filter: {
    progressive(listener, options) {
      // const isCDN = /qiniudn.com/
      // if (isCDN.test(listener.src)) {
      //     listener.el.setAttribute('lazy-progressive', 'true')
      //     listener.loading = listener.src + '?imageView2/1/w/10/h/10'
      // }
    },
    webp(listener, options) {
      // if (!options.supportWebp) return
      // const isCDN = /qiniudn.com/
      // if (isCDN.test(listener.src)) {
      //   listener.src += '?imageView2/2/format/webp'
      // }
      var value = listener.src.toString()
      if (value.slice(-4) === 'jpeg' && value.slice(-5) !== '.jpeg') {
        value = 'https://cube.elemecdn.com/' +
        value.charAt(0) +
        '/' +
        value.slice(1, 3) +
        '/' +
        value.slice(3) +
        '.jpeg?x-oss-process=image/format,webp/resize,w_150'
      } else if (value.slice(-3) === 'png' && value.slice(-4) !== '.png') {
        value = 'https://cube.elemecdn.com/' +
       value.charAt(0) +
       '/' +
       value.slice(1, 3) +
       '/' +
       value.slice(3) +
       '.png?x-oss-process=image/format,webp/resize,w_750'
      } else if (value.slice(-3) === 'jpg' && value.slice(-4) !== '.jpg') {
        value = 'https://cube.elemecdn.com/' +
       value.charAt(0) +
       '/' +
       value.slice(1, 3) +
       '/' +
       value.slice(3) +
       '.jpg?x-oss-process=image/resize,m_lfit,w_120/watermark,g_se,x_4,y_4,image_YS8xYS82OGRlYzVjYTE0YjU1ZjJlZmFhYmIxMjM4Y2ZkZXBuZy5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF8yOA%3D%3D/quality,q_90/format,webp'
      } else if (value.slice(-4) === 'JPEG' && value.slice(-5) !== '.JPEG') {
        value = 'https://cube.elemecdn.com/' +
       value.charAt(0) +
       '/' +
       value.slice(1, 3) +
       '/' +
       value.slice(3) +
       '.JPEG'
      }

      listener.src = value
    }

  }
})
Vue.config.productionTip = false
Vue.prototype.$axios = axios

// axios.defaults.baseURL = 'https://ele-interface.herokuapp.com/'

// 请求拦截
axios.interceptors.request.use(
  config => {
    if (config.method === 'post') {
      config.data = qs.stringify(config.data)
    }

    Toast.loading({
      message: '加载中...',
      forbidClick: true
    })
    // 加载动画
    // Indicator.open()
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截
axios.interceptors.response.use(
  response => {
    // Indicator.close()
    Toast.clear()
    return response
  },
  error => {
    // 错误提醒
    // Indicator.close()
    Toast.clear()
    return Promise.reject(error)
  }
)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
